/**
 * 添加时弹窗
 */
function showAddBox(){
	//清空当前form表单中的信息
	$("#editId").val("");
	$("#editRoleName").textbox("setValue", "");
	$("#editRoleCode").textbox("setValue", "");
	$("#editDescription").textbox("setValue", "");
	$('#edit-role-box').show().window('open');
}

/**
 * 编辑弹窗
 */
function showEditBox(){
	var row = $('#role-list-box').datagrid('getSelected');
	if (row){
		$.ajax({
			type:'POST',
			url:'role/edit_role',
			data:{'id':row.id},
			dataType:'JSON',
			success:function(data){
				$("#editId").val(data.id);
				$("#editRoleName").textbox("setValue", data.roleName);
				$("#editRoleCode").textbox("setValue", data.roleCode);
				$("#editDescription").textbox("setValue", data.description);
				$('#edit-role-box').show().window('open');
			},error:function(data){
				$.messager.alert('Info', data.message, 'info');
			}
		});
	}else{
		alert("请点击选择需要修改的记录！");
	}
}	

/**
 *	提交用户添加表单
 */
function submitSaveRole(){
	$('#edit-role-form').form('submit',{
		onSubmit:function(){
			return $(this).form('validate');
		},
		success:function(data){
			var data = eval('(' + data + ')');  
			$.messager.alert('Info', data.message, 'info');
			$('#edit-role-box').dialog('close');
			$('#role-list-box').datagrid('reload');
		}
	});
}


/**
 *	提交查询
 */
function doSearch(){
	$('#role-list-box').datagrid('load',{
		roleName: $('#roleName').textbox("getValue"),
		roleCode: $('#roleCode').textbox("getValue")
	});
}

/**
 * 重置查询條件
 * @returns
 */
function resetSearchBox(){
	$('#roleName').textbox('setValue','');
	$('#roleCode').textbox('setValue','');
}

/**
 *	刪除选择行
 */
function deleteRole(){
	var row = $('#role-list-box').datagrid('getSelected');
	if (row){
		$.messager.confirm('Confirm','确定刪除记录-角色：' + row.roleName + ' ?' ,function(r){
		    if (r){
		    	$.ajax({
					type:'POST',
					url:'role/delete_role',
					dataType:'JSON',
					data:{'id':row.id},
					success:function(data){
						$.messager.alert('Info', data.message, 'info');
						$('#role-list-box').datagrid('reload');
					},error:function(e){
						console.info(e);
					}
				});
		    }
		});
	}else{
		alert("请点击选择需要刪除的记录！");
	}
}

//配置角色菜单
function editMenu(){
	var row = $('#role-list-box').datagrid('getSelected');
	if (row){
		$.ajax({
			type:'POST',
			async:false,
			url:'role_menu/load_role_menu',
			data:{'roleId':row.id},
			success:function(data){
				data = JSON.parse(data);
				$("#menu-tree").tree({   
				    data:data,
				    cascadeCheck: false,
				    onCheck:function(node, checked){                 						 //当点击 checkbox 时触发
				    	if(checked){
				    		var parent = $("#menu-tree").tree('getParent',node.target); 
				    		if(parent != undefined){
				    			$("#menu-tree").tree('check', parent.target);
				    		}
				    	}
			      	}
				}); 
				$("#edit-role-menu-box").show().window('open');
			},error:function(data){
				$.messager.alert('Info', '操作异常', 'info');
			}
		})
	}else{
		$.messager.alert('錯誤', '请点击选择需要配置的记录！', 'warn');
	}
}

//保存角色菜单配置
function setRoleMenu(){
	var row = $('#role-list-box').datagrid('getSelected');
	var checkedMenuList = $('#menu-tree').tree("getChecked");
   	if(checkedMenuList.length > 0){
   		var menuIds = new Array;
	   	$.each(checkedMenuList,function(i,e){
	   		menuIds.push(e.id);
	   	})
	   	$.ajax({
	   		url:"role_menu/save_role_menu",
	   		type:"POST",
	   		data:{"menuIds":menuIds,"roleId":row.id},
	   		dataType:"JSON",
	   		traditional: true,
	   		beforeSend: function () {
				$.messager.progress({
					title: '提示',
					msg: '系统处理中……',
					text: ''
				});
			},
			complete: function () {
				$.messager.progress('close');
			},
	   		success:function(data){
	   			$.messager.alert("Info", data.message, "info");
	   			$("#edit-role-menu-box").dialog('close');
				$("#role-list-box").datagrid("reload");
	   		},
	   		error:function(e){
	   			console.info(e);
	   		}
	   	})
   	}else{
	   	$.messager.alert('系统提示','未选择任何菜单!','info');
      	return false;
   	}
}


//配置角色权限
function editPermission(){
	var row = $('#role-list-box').datagrid('getSelected');
	if (row){
		$.ajax({
			type:'POST',
			async:false,
			url:'role_permission/load_role_permission',
			data:{'roleId':row.id},
			dataType:"JSON",
			success:function(data){
				var html = "";
				$.each(data,function(i,e){
					html += '<div id="mp' + i +'" class="easyui-panel" title="' + e.menuName + '" style="width:580px;min-height:50px;height:auto;padding:10px;background:#fafafa;" > ';
					$.each(e.list,function(j,e1){
						if(e1.checked){
							html += '<span class="permission"><input type="checkbox" checked="checked" value="' + e1.permissionId + '"/>' + e1.permissionName + '</span>';
						}else{
							html += '<span class="permission"><input type="checkbox" value="' + e1.permissionId + '"/>' + e1.permissionName + '</span>';
						}
					})
					html += '</div>';
				})
				$("#permission-list-box").empty().append(html);
				$.parser.parse($("#permission-list-box"));
				$("#edit-role-permission-box").show().window('open');
			},error:function(data){
				$.messager.alert('Info', '操作异常', 'info');
			}
		})
	}else{
		$.messager.alert('錯誤', '请点击选择需要配置的记录！', 'warn');
	}
}

//保存角色权限
function setRolePermission(){
	var row = $('#role-list-box').datagrid('getSelected');
	var checkedPermissionList = $("#permission-list-box input:checked");
   	if(checkedPermissionList.length > 0){
   		var permissionIds = new Array;
	   	$.each(checkedPermissionList,function(i,e){
	   		permissionIds.push($(e).val());
	   	})
	   	$.ajax({
	   		url:"role_permission/save_role_permission",
	   		type:"POST",
	   		data:{"permissionIds":permissionIds,"roleId":row.id},
	   		dataType:"JSON",
	   		traditional: true,
	   		beforeSend: function () {
				$.messager.progress({
					title: '提示',
					msg: '系统处理中……',
					text: ''
				});
			},
			complete: function () {
				$.messager.progress('close');
			},
	   		success:function(data){
	   			$.messager.alert("Info", data.message, "info");
	   			$("#edit-role-permission-box").dialog('close');
				$("#role-list-box").datagrid("reload");
	   		},
	   		error:function(e){
	   			console.info(e);
	   		}
	   	})
   	}else{
	   	$.messager.alert('系统提示','未选择任何权限!','info');
      	return false;
   	}
}

/**
 * 全选
 */
function selectAll(){
	$("#permission-list-box input[type='checkbox']").each(function(i,e){
		$(e).prop("checked",true);
	})
}

/**
 * 取消
 */
function cancleAll(){
	$("#permission-list-box input[type='checkbox']").each(function(i,e){
		$(e).prop("checked",false);
	})
}